﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Static Methods and Attributes</title>
    <script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="../js/Interface.js"></script>
    <script type="text/javascript" src="../js/Helpers.js"></script>
</head>
<body>
    <div id="content">
    </div>
    <script type="text/javascript">
        var Book = (function () {
            // Private static attributes.
            var numOfBooks = 0;
            // Private static method.
            function checkIsbn(isbn) {
                return true;
            }
            // Return the constructor.
            return function (newIsbn, newTitle, newAuthor) { // implements Publication
                // Private attributes.
                var isbn, title, author;
                // Privileged methods.
                this.getIsbn = function () {
                    return isbn;
                };
                this.setIsbn = function (newIsbn) {
                    if (!checkIsbn(newIsbn)) throw new Error('Book: Invalid ISBN.');
                    isbn = newIsbn;
                };
                this.getTitle = function () {
                    return title;
                };
                this.setTitle = function (newTitle) {
                    title = newTitle || 'No title specified';
                };
                this.getAuthor = function () {
                    return author;
                };
                this.setAuthor = function (newAuthor) {
                    author = newAuthor || 'No author specified';
                };
                // Constructor code.
                numOfBooks++; // Keep track of how many Books have been instantiated
                // with the private static attribute.
                if (numOfBooks > 50) throw new Error('Book: Only 50 instances of Book can be ' + 'created.');
                this.setIsbn(newIsbn);
                this.setTitle(newTitle);
                this.setAuthor(newAuthor);
            }
        })();
        // Public static method.
        Book.convertToTitleCase = function (inputString) {
            return inputString.toUpperCase()
        };
        // Public, non-privileged methods.
        Book.prototype = {
            display: function () {
                return String.format("{0} {1} {2}", this.getIsbn(), this.getTitle(), this.getAuthor());
            }
        };

        $(function () {
            // Book(isbn, title, author)
            var theHobbit = new Book('0-395-07122-4', 'The Hobbit', 'J. R. R. Tolkien');
            $("#content").html(Book.convertToTitleCase(theHobbit.display()));
        });
    </script>
</body>
</html>
